package com.yonyou.iuap.controller.pc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.base.intf.IUserAdminService;
import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.BdIndustrychartProductEntity;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IIndustrychartproductService;

/**
 * 关键字与产品关系维护
 * @author ganchj
 *
 */
@Controller
@RequestMapping("/pc/industryProduct")
public class PCIndustryProductController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IIndustrychartproductService industrychartproductService;
	@Autowired
	private IUserAdminService userService;
	/**
	 * 增改合并到一个方法中
	 * @param parMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public @ResponseBody String insert(@RequestBody Map<String, Object> parMap,
			HttpServletRequest request, HttpServletResponse response) {
		start=System.currentTimeMillis();
		/*系统管理员权限校验 begin*/
		ResultDto userDto = userService.checkUserIsSysManager(parMap.get("userid")==null?"":parMap.get("userid").toString());
		if(userDto.getStatus()!=0){
			return JsonUtils.toJsonStr(userDto);
		}
		/*系统管理员权限校验 end*/
		ResultDto resultDto = new ResultDto();
		if (parMap.get("pk_industry_chart") != null
				&& parMap.get("pk_product_list") != null) {
			
			String del="pk_industry_chart='"+parMap.get("pk_industry_chart").toString()+"'";
			industrychartproductService.deleteByCondition(del);
			
			ArrayList<String> list=(ArrayList<String>) parMap.get("pk_product_list");
			for(String pk_product:list){
				
				String sql = "pk_product='" + pk_product
						+ "' AND pk_industry_chart='"
						+ parMap.get("pk_industry_chart").toString()+"'";
				int count=industrychartproductService.getCount(sql);
				if(count>0){
					resultDto.setStatus(1);
					resultDto.setMessage("产品已存在");
					return JsonUtils.toJsonStr(resultDto);
				}
				BdIndustrychartProductEntity bean = new BdIndustrychartProductEntity();
				bean.setPk_industrychart_product(IDFactory.getInstance().getID());
				bean.setPk_industry_chart(parMap.get("pk_industry_chart")
						.toString());
				bean.setPk_product(pk_product);
				bean.setPk_creator(parMap.get("pk_user").toString());

				industrychartproductService.insert(bean);
			}

			resultDto.setStatus(0);
			resultDto.setMessage("新增成功");
		} else {
			resultDto.setStatus(1);
			resultDto.setMessage("新增失败");
		}
		end=System.currentTimeMillis();
		logger.info("insert接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(resultDto);
	}

	//单独移除使用
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody String delete(@RequestBody Map<String, Object> parMap,
			HttpServletRequest request, HttpServletResponse response) {
		start=System.currentTimeMillis();
		/*系统管理员权限校验 begin*/
		ResultDto userDto = userService.checkUserIsSysManager(parMap.get("userid")==null?"":parMap.get("userid").toString());
		if(userDto.getStatus()!=0){
			return JsonUtils.toJsonStr(userDto);
		}
		/*系统管理员权限校验 end*/
		ResultDto resultDto = new ResultDto();

		if (parMap.get("pk_industry_chart") != null
				&& parMap.get("pk_product_list") != null) {
			ArrayList<String> list=(ArrayList<String>) parMap.get("pk_product_list");
			for(String pk_product:list){
				String sql = "pk_product=" + pk_product
						+ " AND pk_industry_chart="
						+ parMap.get("pk_industry_chart").toString();
				industrychartproductService.deleteByCondition(sql);
			}

			resultDto.setStatus(0);
			resultDto.setMessage("删除成功");
		} else {
			resultDto.setStatus(1);
			resultDto.setMessage("删除失败");
		}
		end=System.currentTimeMillis();
		logger.info("delete接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(resultDto);
	}
	
}
